package com.bmp.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.bmp.dao.CasesDao;
import com.bmp.model.Cases;
import com.bmp.model.vo.CaseVoProjectUser;
import com.bmp.util.HibernateUtil;

@Component("casesDao")
public class CasesDaoImpl implements CasesDao {
	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void deleteCasesById(int id) {
		Cases c = this.getCasesById(id);
		this.hibernateTemplate.delete(c);
	}

	public Cases getCasesById(int id) {
		return (Cases)this.hibernateTemplate.get(Cases.class, id);
	}

	public void insertCases(Cases c) {
		this.hibernateTemplate.save(c);
	}

	public void updateCases(Cases c) {
		this.hibernateTemplate.update(c);
	}
	//获取每页显示的用例信息
	@SuppressWarnings("unchecked")
	public List<CaseVoProjectUser> getCases(int page, int rows) {
		List<CaseVoProjectUser> list = null;
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		String sql = "select new com.bmp.model.vo.CaseVoProjectUser(c.id,c.name,p.p_name,c.proce,c.expect,c.actual,u.name,u1.name,c.build_date,c.test_date,c.status) from Cases c,Project p,User u,User u1 where c.pro_id = p.id and c.builder_id = u.id and c.appoint_id = u1.id";
		list = session.createQuery(sql).setFirstResult((page-1)*rows).setMaxResults(rows).list();
		session.getTransaction().commit();
		return list;
	}
	//获取总页数
	public int getTotalPages() {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		session.beginTransaction();
		String sql = "select count(*) from Cases c";
		long count = (Long)session.createQuery(sql).uniqueResult();
		session.getTransaction().commit();
		//System.out.println(count);
		return (int)count;
	}

}
